package com.doublefish.doublefishrobot;

import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.doublefish.doublefishrobot.base.BaseActivity;
import com.doublefish.doublefishrobot.ui.home.HomeContentFragment;
import com.doublefish.doublefishrobot.ui.home.HomeFragment;
import com.doublefish.doublefishrobot.ui.setting.LocalPdfReaderActivity;
import com.doublefish.doublefishrobot.ui.setting.SettingFragment;
import com.doublefish.doublefishrobot.utils.AppCacheUtil;
import com.doublefish.doublefishrobot.utils.db.DataBaseApi;
import com.doublefish.doublefishrobot.widget.PrivacyDialog;

import butterknife.BindView;

/**
 * Created by peak on 2024/10/15
 */
public class MainActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener {

    @BindView(R.id.navigation_bar)
    BottomNavigationBar mNavigationBar;

    private BottomNavigationItem homeItem;
//    private BottomNavigationItem videoItem;
    private BottomNavigationItem settingItem;

////    private HomeFragment homeFragment = new HomeFragment();
    private HomeContentFragment homeFragment = new HomeContentFragment();
//    private VideoFragment videoFragment = new VideoFragment();
    private SettingFragment settingFragment = new SettingFragment();

    private boolean isShowHomeContentFragment = true;  //是否显示HomeContentFragment
    private boolean isShowPrivacyDialog = false;  //是否已经显示显示隐私弹窗

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        homeItem = new BottomNavigationItem(R.mipmap.f_1, getString(R.string.tab_home)).setActiveColorResource(R.color.colorPrimary).setInactiveIconResource(R.mipmap.f_2).setInActiveColorResource(R.color.grey1);
//        videoItem = new BottomNavigationItem(R.mipmap.a_1, "视频").setActiveColorResource(R.color.colorPrimary).setInactiveIconResource(R.mipmap.a_2).setInActiveColorResource(R.color.grey1);
        settingItem = new BottomNavigationItem(R.mipmap.d_1, getString(R.string.tab_setting)).setActiveColorResource(R.color.colorPrimary).setInactiveIconResource(R.mipmap.d_2).setInActiveColorResource(R.color.grey1);

        mNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        mNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_DEFAULT);
        mNavigationBar.addItem(homeItem)
//                .addItem(videoItem)
                .addItem(settingItem)
                .setFirstSelectedPosition(0)
                .initialise();

        setDefaultFragment();
    }

    @Override
    protected void initListener() {
        mNavigationBar.setTabSelectedListener(this);

        homeFragment.setOnCheckPermissonsListner(new HomeFragment.OnCheckPermissonsListner() {
            @Override
            public boolean onCheckBlePermission() {
                return checkBlePermission();
            }

            @Override
            public boolean onCheckWifiPermission() {
                return checkWIFIPermission();
            }
        });
    }

    @Override
    protected void initData() {
        //创建数据库
        DataBaseApi.createDB(getApplicationContext());
    }

    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
//        transaction.replace(R.id.ll_content, homeFragment).commit();

        transaction.add(R.id.ll_content, homeFragment);
        transaction.add(R.id.ll_content, settingFragment);
        transaction.hide(settingFragment).show(homeFragment).commit();
    }

    @Override
    protected void onDestroy() {
        Log.i("Main", "onDestroy");
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (AppCacheUtil.getInstance().isDidReadPrivacy()) {
            return;
        }
        if (isShowPrivacyDialog) {
            return;
        }
        isShowPrivacyDialog = true;
        PrivacyDialog dialog = new PrivacyDialog(this, new PrivacyDialog.OnBtnClickListener() {
            @Override
            public void onPrivacyBtnClick() {
                toPrivacyActivity();
            }

            @Override
            public void onConfirmBtnClick() {
                isShowPrivacyDialog = false;
                AppCacheUtil.getInstance().setDidReadPrivacy(true);
            }

            @Override
            public void onCancelBtnClick() {
                isShowPrivacyDialog = false;
                exitApp();
            }
        });
        dialog.show();
    }

    private void exitApp() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    private void toPrivacyActivity() {
        String fileName = getString(R.string.privacy_file_name);
        String title = getString(R.string.privacy);
        Intent intent = LocalPdfReaderActivity.makeIntent(this, fileName, title);
        startActivity(intent);
    }

    //===================NavigationBar监听开始===================
    @Override
    public void onTabSelected(int position) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        switch (position){
            case 0:
                isShowHomeContentFragment = true;
                transaction
                        .hide(settingFragment)
                        .show(homeFragment)
                        .commit();
            break;
            case 1:
                isShowHomeContentFragment = false;
                transaction
                        .hide(homeFragment)
                        .show(settingFragment)
                        .commit();
                break;
        }
    }
    @Override
    public void onTabUnselected(int position) {
    }
    @Override
    public void onTabReselected(int position) {
    }
    //===================NavigationBar监听结束===================


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //返回按钮事件
            if (isShowHomeContentFragment && homeFragment.onBackKeyDown()) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
